package com.ssh.nio.channel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class ByteChannelTest {
	public static void main(String[] args) throws IOException {
		ReadableByteChannel src = Channels.newChannel(System.in);
		WritableByteChannel desc = Channels.newChannel(System.out);

		copyChannel(src, desc);

		src.close();
		desc.close();
	}

	private static void copyChannel(ReadableByteChannel src,
			WritableByteChannel desc) throws IOException {
		// TODO Auto-generated method stub

		ByteBuffer buffer = ByteBuffer.allocate(16);

		while (src.read(buffer) != -1) {
			System.out.println("---start---");
			buffer.flip();
			desc.write(buffer);
			buffer.compact();
		}

		buffer.flip();
		while (buffer.hasRemaining()) {
			System.out.println("----未写完--------");
			desc.write(buffer);
		}
	}

}
